package com.gt.task;

import com.gt.mapper.BorrowMapper;
import com.gt.pojo.entity.Borrow;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;

@Component
@Slf4j
public class BorrowTask {

    @Autowired
    private BorrowMapper borrowMapper;

    //每天14点触发
    @Scheduled(cron = "0 0/30 * * * ?")
    /* @Scheduled(cron = "0/5 * * * * ?")*/
    public void processTimoutReturn(){
        log.info("定时处理处于借阅中的书籍",LocalDateTime.now());
        LocalDateTime now = LocalDateTime.now();
        List<Borrow> borrowList = borrowMapper.getByStatusAndEndTimeLT(Borrow.BORROWING, now);
        for (Borrow borrow : borrowList) {
            borrow.setStatus(Borrow.ERROR);
            borrow.setErrorReason("还书超时");
            borrowMapper.update(borrow);
        }
    }
    @Scheduled(cron = "0 0/30 * * * ?")
    /*@Scheduled(cron = "0/5 * * * * ?")*/
    public void processTimoutRenew(){
        log.info("定时处理处于续借中的书籍",LocalDateTime.now());
        LocalDateTime now = LocalDateTime.now();
        List<Borrow> borrowList = borrowMapper.getByStatusAndEndTimeLT(Borrow.RENEWING, now);
        for (Borrow borrow : borrowList) {
            borrow.setStatus(Borrow.ERROR);
            borrow.setErrorReason("续借超时");
            borrowMapper.update(borrow);
        }
    }
}
